<?php

namespace App\Http\Controllers\RBAC;

use App\RBAC\Category;
use App\RBAC\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;

class BlogController extends Controller
{
 
     /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }



    public function fileupload(Request $request){

        $filesCount=$request->filesCount;

        $paths=[];

        for($i=0;$i<$filesCount;$i++){
            $path = $request->file('img_'.$i)->store('/public/'.date('Y-m-d').'');
            array_push($paths,Storage::url($path));
        }

        return $paths;
    }


    public function article(Request $request){
        $user=Auth::user();
        $data['categories']=$user->categories;
        $article=Article::find(intval($request->currentId));
        $hasCat=false;
        $page=1;
        $count=0;
        if($article){
            $hasCat=true;
            $article->category;

            $count=Article::where('user_id',$user->id)->where('status','!=','trashed')->where('category_id',$article->category_id)->where('id','<=',$article->id)->count();
            $page=ceil(1.0*$count/10);
        }
        $data['page']=$page;
        $data['hasCat']=$hasCat;
        $data['currentArticle']=$article;
        return $data;
    }

    public function articleMove(Request $request){
        $article_id=0;
        $user=Auth::user(); 
        if($request->status && $request->status=='delete'){
            $deletedRows=Article::where('user_id',$user->id)->where('status','trashed')->delete();
            if($deletedRows){
                return ['msg'=>'success','deletedRows'=>$deletedRows];
            }else{
                return ['msg'=>'failure'];
            }
        }


        if($request->article_id){
            $article_id=$request->article_id;
        }
        $article=Article::find($article_id);
        if($request->category_id){
            $article->category_id=$request->category_id;
        }
        if($request->status){
            $article->status=$request->status;
        }  
        $data=[];
        if($article->save()){
            $data['msg']='success';
        }else{
            $data['msg']='移动文章失败';
        }
        return $data;
    }

    public function articleStore(Request $request){
        $user=Auth::user(); 
        $item=null;
        if($request->article_id){
            $item=Article::find($request->article_id);
            $item->title=$request->title;
            $item->status=$request->status;
            $item->html=$request->html;
            $item->description=mb_substr(strip_tags($item->html),0,80);
            $item->markdown=$request->markdown;
            $item->user_id=$user->id;
            $item->category_id=$request->category_id;
            $item->save();
        }else{
            $item=new Article();
            $item->title=$request->title;
            $item->status=$request->status;
            $item->html=$request->html;
            $item->description=mb_substr(strip_tags($item->html),0,80);
            $item->markdown=$request->markdown;
            $item->user_id=$user->id;
            $item->category_id=$request->category_id;
            $item->save();
        }
        return $item;
    }

    public function categoryStore(Request $request){
        $user=Auth::user();        
        $cat=null;
        $catitem=$request->catitem;

        if($request->action && $request->action=='delete'){
            $cat=Category::find($request->category_id);
            $deletedRows=Article::where('user_id',$user->id)->where('category_id',$cat->id)->where('status','trashed')->delete();
            $ret=$cat->delete();
            if($ret){
                return ['msg'=>'success','deletedRows'=>$deletedRows];
            }else{
                return ['msg'=>'failure'];
            }
        }

        if(isset($catitem['id'])){
            $cat=Category::find($catitem['id']);
            $cat->name=$catitem['name'];
            $cat->save();
        }else{
            $cat=new Category();
            $cat->name=$catitem['name'];
            $cat->user_id=$user->id;
            $cat->save();
        }
        return $cat;
    }

    public function articleList(Request $request){
        $user=Auth::user(); 
        $category_id=$request->category_id;
        if($category_id=='trashed'){
          $data=Article::where('status','trashed')->where('user_id',$user->id)->orderBy('id',"ASC")->paginate($request->per_page);  
        }else{
          $data=Article::where('category_id',$category_id)->where('status','!=','trashed')->where('user_id',$user->id)->orderBy('id',"ASC")->paginate($request->per_page);  
        }
        return $data;

    }

}
